package com.CNMedical.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.diboot.core.service.impl.BaseServiceImpl;
import com.CNMedical.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.CNMedical.system.mapper.AppletUserMapper;
import com.CNMedical.system.domain.entity.AppletUser;
import com.CNMedical.system.service.IAppletUserService;

import javax.annotation.Resource;

/**
 * 用户信息Service业务层处理
 * 
 * @author tang
 * @date 2023-04-03
 */
@Service
public class AppletUserServiceImpl extends BaseServiceImpl<AppletUserMapper,AppletUser> implements IAppletUserService
{
    @Resource
    private AppletUserMapper appletUserMapper;

    @Override
    public boolean editAppletUser(AppletUser appletUser) {
        return saveOrUpdate(appletUser);
    }

    @Override
    public boolean checkUserNameUnique(AppletUser user) {
        return lambdaQuery().eq(AppletUser::getUserName,user.getUserName()).exists();
    }

    @Override
    public boolean checkPhoneUnique(AppletUser user) {
        return lambdaQuery().eq(AppletUser::getPhonenumber,user.getPhonenumber()).exists();
    }

    @Override
    public boolean checkEmailUnique(AppletUser user) {
        return lambdaQuery().eq(AppletUser::getEmail,user.getEmail()).exists();
    }

    @Override
    public List<Long> getUserIdByUserName(String userName) {
        List<AppletUser> appletUserList = lambdaQuery().like(AppletUser::getUserName, userName).list();
        final List<Long> userIdList = appletUserList.stream().map(AppletUser::getId).collect(Collectors.toList());
        return userIdList;
    }

    /**
     * 查询用户信息
     * 
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public AppletUser selectAppletUserById(Long id)
    {
        return appletUserMapper.selectAppletUserById(id);
    }

    /**
     * 查询用户信息列表
     * 
     * @param appletUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<AppletUser> selectAppletUserList(AppletUser appletUser)
    {
        return appletUserMapper.selectAppletUserList(appletUser);
    }

    /**
     * 新增用户信息
     * 
     * @param appletUser 用户信息
     * @return 结果
     */
    @Override
    public int insertAppletUser(AppletUser appletUser)
    {
        appletUser.setCreateTime(DateUtils.getNowDate());
        return appletUserMapper.insertAppletUser(appletUser);
    }

    /**
     * 修改用户信息
     * 
     * @param appletUser 用户信息
     * @return 结果
     */
    @Override
    public int updateAppletUser(AppletUser appletUser)
    {
        appletUser.setUpdateTime(DateUtils.getNowDate());
        return appletUserMapper.updateAppletUser(appletUser);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteAppletUserByIds(Long[] ids)
    {
        return appletUserMapper.deleteAppletUserByIds(ids);
    }

    /**
     * 删除用户信息信息
     * 
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteAppletUserById(Long id)
    {
        return appletUserMapper.deleteAppletUserById(id);
    }

    /**
     * 微信登录注册
     * @param appletUser
     * @return
     */
    @Override
    public AppletUser wxLogin(AppletUser appletUser) {

        // 根据openid判断用户是否存在
        AppletUser param = new AppletUser();
        param.setOpenId(appletUser.getOpenId());
        List<AppletUser> appletUsers = appletUserMapper.selectAppletUserList(param);
        if(appletUsers.isEmpty()){
            // 注册
            appletUserMapper.insertAppletUser(appletUser);

            // 查询出来
            appletUsers = appletUserMapper.selectAppletUserList(appletUser);
        }

        AppletUser user = appletUsers.get(0);

        return user;
    }
}
